/**
 * @file pugixml_test01.cpp
 * @author 逆流 (1171267147@qq.com)
 * @brief pugixml xpath接口测试
 * @version 0.1
 * @date 2024-08-02
 *
 * @copyright Copyright (c) 2024.
 *
 */

#include <iostream>
#include <string>

#include "env.h"
#include "pugixml/pugixml.hpp"

int main() {
    pugi::xml_document doc;
    std::string        file = kResourcesDir + "DevAlarmCfg.xml";

    auto result = doc.load_file(file.c_str());
    if (!result) {
        std::cout << "load file failed" << '\n';
        return 0;
    }

    // 通过xpath获取指定节点
    pugi::xpath_node node = doc.select_node("/TerminalAlarmIdCfg/Base/VerNO");
    if (node != nullptr) {
        std::cout << "node name: " << node.node().name() << '\n';
        // 遍历节点属性
        for (auto attr : node.node().attributes()) {
            std::cout << "attr name: " << attr.name() << " attr value: " << attr.value() << '\n';
        }
    } else {
        std::cout << "node not found" << '\n';
    }

    // pugixml支持通过xpath格式直接访问属性节点
    pugi::xpath_node attr = doc.select_node("/TerminalAlarmIdCfg/Base/VerNO[@Value]");
    if (attr != nullptr) {
        std::cout << "attr name: " << attr.node().name() << '\n';
        std::cout << "attr value: " << attr.node().first_attribute().value() << '\n';
    }

    pugi::xpath_node arrayAttr = doc.select_node("/TerminalAlarmIdCfg/AlarmIdInfo/Alarm[2][@Id]");
    if (arrayAttr != nullptr) {
        std::cout << "arrayAttr name: " << arrayAttr.node().name() << '\n';
        std::cout << "arrayAttr value: " << arrayAttr.node().first_attribute().value() << '\n';
    }

    return 0;
}
